#ifndef __MAIN_H
#define __MAIN_H

#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define YEAR ((((__DATE__[7] - '0')*10 + (__DATE__[8] - '0'))*10 + (__DATE__[9] - '0'))*10 + (__DATE__[10] - '0'))
#define MONTH (__DATE__[2] == 'n' ? 0\
              :__DATE__[2] == 'b' ? 1\
              :__DATE__[2] == 'r' ? (__DATE__[0] == 'M' ? 2 : 3)\
              :__DATE__[2] == 'y' ? 4\
              :__DATE__[2] == 'n' ? 5\
              :__DATE__[2] == 'l' ? 6\
              :__DATE__[2] == 'g' ? 7\
              :__DATE__[2] == 'p' ? 8\
              :__DATE__[2] == 't' ? 9\
              :__DATE__[2] == 'v' ? 10 : 11)
#define DAY ((__DATE__[4] == ' ' ? 0 : __DATE__[4] - '0')*10 + (__DATE__[5] - '0'))

#define HOUR    ((__TIME__[0] - '0')*10 + __TIME__[1] - '0')
#define MINUTE  ((__TIME__[3] - '0')*10 + __TIME__[4] - '0')
#define SECOND  ((__TIME__[6] - '0')*10 + __TIME__[7] - '0')

#define TIME_OUT_1MS   (2)
#define TIME_OUT_2MS   (2*TIME_OUT_1MS)
#define TIME_OUT_5MS   (5*TIME_OUT_1MS)
#define TIME_OUT_10MS  (10*TIME_OUT_1MS)
#define TIME_OUT_20MS  (20*TIME_OUT_1MS)
#define TIME_OUT_50MS  (50*TIME_OUT_1MS)
#define TIME_OUT_100MS (100*TIME_OUT_1MS)
#define TIME_OUT_200MS (200*TIME_OUT_1MS)
#define TIME_OUT_250MS (250*TIME_OUT_1MS)
#define TIME_OUT_500MS (500*TIME_OUT_1MS)
#define TIME_OUT_1S    (1000*TIME_OUT_1MS)
#define TIME_OUT_30S   (30*TIME_OUT_1S)

//lwip控制结构体
typedef struct  
{
	uint8_t mac[6];      //MAC地址
	uint8_t remoteip[4]; //远程主机IP地址
	uint8_t ip[4];       //本地IP地址
	uint8_t netmask[4];  //子网掩码
	uint8_t gateway[4];  //默认网关地址
	
	uint8_t dhcpstatus;	//dhcp状态
					//0,未获取DHCP地址
					//1,进入DHCP获取状态
					//2,成功获取DHCP地址
					//0XFF,获取失败
}__lwip_dev_t;


#define SYSTEMTICK_PERIOD_MS  10
//#define USE_DHCP 1
//#define USE_ENET_INTERRUPT

/* MAC address: MAC_ADDR0:MAC_ADDR1:MAC_ADDR2:MAC_ADDR3:MAC_ADDR4:MAC_ADDR5 */
#define MAC_ADDR0   2//0x00
#define MAC_ADDR1   0xA//0x80
#define MAC_ADDR2   0xF//0xE1
#define MAC_ADDR3   0xE//0x00
#define MAC_ADDR4   0xD//0x00
#define MAC_ADDR5   6//0x00
 
/* static IP address: IP_ADDR0.IP_ADDR1.IP_ADDR2.IP_ADDR3，HEM6801-IP地址*/
#define IP_ADDR0   192
#define IP_ADDR1   168
#define IP_ADDR2   1
#define IP_ADDR3   100
//端口
#define LOCAL_TCP_SERVER_PORT 502   /*tcp 服务器端口*/
#define LOCAL_TCP_CLIENT_PORT 10260
#define LOCAL_UDP_PORT        1025
/* remote IP address: IP_S_ADDR0.IP_S_ADDR1.IP_S_ADDR2.IP_S_ADDR3 */
#define IP_S_ADDR0   192
#define IP_S_ADDR1   168
#define IP_S_ADDR2   1
#define IP_S_ADDR3   101
   
/* net mask,掩码*/
#define NETMASK_ADDR0   255
#define NETMASK_ADDR1   255
#define NETMASK_ADDR2   255
#define NETMASK_ADDR3   0

/* gateway address，网关*/
#define GW_ADDR0   192
#define GW_ADDR1   168
#define GW_ADDR2   1
#define GW_ADDR3   1

/* MII and RMII mode selection */
#define RMII_MODE  // user have to provide the 50 MHz clock by soldering a 50 MHz oscillator
//#define MII_MODE

/* clock the PHY from external 25MHz crystal (only for MII mode) */
#ifdef  MII_MODE
#define PHY_CLOCK_MCO
#endif

enum
{
    eDisable,
    eEnable
};

extern uint32_t ul_tick_value;
extern uint8_t g_ai_com_ch;
extern uint8_t g_ai_select_sn;
extern uint8_t uc_enet_is_valid;
extern uint8_t uc_enet_status_flag;//常亮：以太网连接成功；常灭：以太网连接失败；

extern uint32_t g_year;
extern uint32_t g_month;
extern uint32_t g_day;
extern uint32_t g_hour;
extern uint32_t g_minute;
extern uint32_t g_second;

extern int g_tcpClient_sockfd;
extern uint8_t uc_rd_is_valid;/*0:无线无效，1：无线有效*/
void system_tick(void);
void Main_Reset(void);

void time_update(void);
void delay_10ms(uint32_t ncount);

#endif /* __MAIN_H */


